package net.lab1024.sa.admin.module.fabricmaterials.controller;

import net.lab1024.sa.admin.module.fabricmaterials.domain.entity.FabricMaterialsEntity;
import net.lab1024.sa.admin.module.fabricmaterials.domain.form.FabricMaterialsAddForm;
import net.lab1024.sa.admin.module.fabricmaterials.domain.form.FabricMaterialsQueryForm;
import net.lab1024.sa.admin.module.fabricmaterials.domain.form.FabricMaterialsUpdateForm;
import net.lab1024.sa.admin.module.fabricmaterials.domain.vo.FabricMaterialsVO;
import net.lab1024.sa.admin.module.fabricmaterials.service.FabricMaterialsService;
import net.lab1024.sa.base.common.domain.ValidateList;
import net.lab1024.sa.base.common.util.SmartExcelUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import cn.dev33.satoken.annotation.SaCheckPermission;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * lyy Controller
 *
 * @Author lyy
 * @Date 2024-11-12 22:01:35
 * @Copyright lyy
 */

@RestController
@Tag(name = "耗材管理")
public class FabricMaterialsController {

    @Resource
    private FabricMaterialsService fabricMaterialsService;

    @Operation(summary = "分页查询 @author lyy")
    @PostMapping("/fabricMaterials/queryPage")
    @SaCheckPermission("fabricMaterials:query")
    public ResponseDTO<PageResult<FabricMaterialsVO>> queryPage(@RequestBody @Valid FabricMaterialsQueryForm queryForm) {
        return ResponseDTO.ok(fabricMaterialsService.queryPage(queryForm));
    }

    @Operation(summary = "添加 @author lyy")
    @PostMapping("/fabricMaterials/add")
    @SaCheckPermission("fabricMaterials:add")
    public ResponseDTO<String> add(@RequestBody @Valid FabricMaterialsAddForm addForm) {
        return fabricMaterialsService.add(addForm);
    }

    @Operation(summary = "更新 @author lyy")
    @PostMapping("/fabricMaterials/update")
    @SaCheckPermission("fabricMaterials:update")
    public ResponseDTO<String> update(@RequestBody @Valid FabricMaterialsUpdateForm updateForm) {

        return fabricMaterialsService.update(updateForm);
    }

    @Operation(summary = "批量删除 @author lyy")
    @PostMapping("/fabricMaterials/batchDelete")
    @SaCheckPermission("fabricMaterials:delete")
    public ResponseDTO<String> batchDelete(@RequestBody ValidateList<String> idList) {
        return fabricMaterialsService.batchDelete(idList);
    }

    @Operation(summary = "单个删除 @author lyy")
    @GetMapping("/fabricMaterials/delete/{id}")
    @SaCheckPermission("fabricMaterials:delete")
    public ResponseDTO<String> batchDelete(@PathVariable String id) {
        return fabricMaterialsService.delete(id);
    }

    @Operation(summary = "物料信息")
    @GetMapping("/fabricMaterials/getFabricMaterials/{fabricMaterialCode}")
    @SaCheckPermission("fabricMaterials:query")
    public ResponseDTO<FabricMaterialsVO> getFabricMaterials(@PathVariable  String fabricMaterialCode){
        return fabricMaterialsService.getFabricMaterials(fabricMaterialCode);
    }
//    所有物料信息
    @Operation(summary = "所有物料信息")
    @GetMapping("/fabricMaterials/queryAll")
    @SaCheckPermission("fabricMaterials:query")
    public ResponseDTO<List<FabricMaterialsVO>> queryAll() {
        return fabricMaterialsService.queryAll();
    }

//    获取所有的物料代码

    @Operation(summary = "获取所有的物料代码")
    @GetMapping("/fabricMaterials/getAllMaterialsCode")
    @SaCheckPermission("fabricMaterials:query")
    public ResponseDTO<List<FabricMaterialsVO>> getAllFabricMaterials() {
        return fabricMaterialsService.getAllFabricMaterials();
    }

    @Operation(summary = "查看物料代码")
    @GetMapping("/fabricMaterials/getCode/{code}")
    @SaCheckPermission("fabricMaterials:getCode")
    public ResponseDTO<String> getCode(@PathVariable String code) {
        return fabricMaterialsService.getFabricMaterialCode(code);
    }

    @Operation(summary = "查询库存")
    @GetMapping("/fabricMaterials/getFabricMaterial/{id}")
    public ResponseDTO<FabricMaterialsEntity> getFabricById(@PathVariable @Valid Integer id){
        return fabricMaterialsService.getFabricById(id);
    }

    @Operation(summary = "导出物料")
    @GetMapping("/fabricMaterials/export")
    public void export(HttpServletResponse response) throws IOException {
        List<FabricMaterialsVO> allFabricMaterials = fabricMaterialsService.selectList();
         SmartExcelUtil.exportExcel(response,"物料库存.xlsx","物料库存",FabricMaterialsVO.class,allFabricMaterials);

    }


}
